{% extends "base.html" %}
{% block title %}项目信息{% endblock %}

{% block content %}

    <div class="am-modal am-modal-confirm" tabindex="-1" id="my-invalid">
        <div class="am-modal-dialog">
            <div class="am-modal-hd">柠檬提醒</div>
            <div class="am-modal-bd">
                亲，此操作会强制删除该项目下所有接口和用例，请谨慎操作！！！
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="am-modal-btn" data-am-modal-confirm>确定</span>
            </div>
        </div>
    </div>

    <div class="am-modal am-modal-confirm" tabindex="-1" id="select_case">
        <div class="am-modal-dialog">
            <div class="am-modal-hd">柠檬提醒</div>
            <div class="am-modal-bd">
                确认执行测试用例吗？
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="am-modal-btn" data-am-modal-confirm>确定</span>
            </div>
        </div>
    </div>

    <div class="admin-biaogelist">
        <div class="listbiaoti am-cf">
            <ul class="am-icon-flag on"> 项目列表</ul>
            <dl class="am-icon-home" style="float: right;"> 当前位置： 项目管理 > <a href="#">项目展示</a></dl>
            <dl>
                <button type="button" class="am-btn am-btn-danger am-round am-btn-xs am-icon-plus"
                        onclick="location='{{ url_for('web.create_project') }}'">新增项目
                </button>
                <button type="button" class="am-btn am-btn-danger am-round am-btn-xs am-icon-bug"
                        onclick="run_test_batch('project')">运行
                </button>
            </dl>
        </div>

        <form class="am-form am-g" id="case_list" name="case_list" method="post" action="/api/run_batch_test/">
            <table width="100%" class="am-table am-table-bordered am-table-radius am-table-striped">
                <thead>
                <tr class="am-success">
                    <th class="table-check"><input type="checkbox" id="select_all"/></th>
                    <th class="table-title">序号</th>
                    <th class="table-type">项目名称</th>
                    <th class="table-type">项目简介</th>
                    <th class="table-title">接口/用例 数据</th>
                    <th class="table-date am-hide-sm-only">创建时间</th>
                    <th width="163px" class="table-set">操作</th>
                </tr>
                </thead>
                <tbody>

                {% for foo in projects %}
                    <tr>
                        <td><input type="checkbox" name="project_{{ foo.id }}" value="{{ foo.id }}"/></td>
                        <td>{{ loop.index }}</td>
                        <td><a href="{{ url_for('web.edit_project', p_id=foo.id) }}">{{ foo.project_name }}</a>
                        </td>
                        <td>{{ foo.simple_desc[:20] }}...</td>
                        <td>1/3</td>
                        <td class="am-hide-sm-only">{{ foo.created_at | str_time }}</td>
                        <td>
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button type="button"
                                            class="am-btn am-btn-default am-btn-xs am-text-secondary am-round"
                                            data-am-popover="{content: '运行', trigger: 'hover focus'}"
                                            onclick="run_test('{{ foo.id }}', 'project')">
                                        <span class="am-icon-bug"></span></button>
                                    <button type="button"
                                            class="am-btn am-btn-default am-btn-xs am-text-secondary am-round"
                                            data-am-popover="{content: '编辑', trigger: 'hover focus'}"
                                            onclick="location='{{ url_for('web.edit_project', p_id=foo.id) }}'"> <span
                                            class="am-icon-pencil-square-o"></span></button>
                                    <button type="button"
                                            class="am-btn am-btn-default am-btn-xs am-text-danger am-round"
                                            data-am-popover="{content: '删除', trigger: 'hover focus'}"
                                            onclick="invalid('{{ foo.id }}')"><span
{#                                            onclick="location='{{ url_for('web.delete_project', p_id=foo.id) }}'"><span#}
                                            class="am-icon-trash-o"></span></button>
                                </div>
                            </div>
                        </td>
                    </tr>
                {% endfor %}


                </tbody>
            </table>

            <div class="am-btn-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default" onclick="location='{{ url_for('web.create_project') }}'"><span
                        class="am-icon-plus"></span> 新增
                </button>
            </div>

            <ul class="am-pagination am-fr">
                {% if paginate.has_prev %}
                    <a href="{{ url_for('web.list_projects', page=paginate.prev_num) }}">&lt;上一页</a>
                {% else %}
                    &lt;上一页
                {% endif %}

                {{ paginate.page }}

                {% if paginate.has_next %}
                    <a href="{{ url_for('web.list_projects', page=paginate.next_num) }}">下一页&gt;</a>
                {% else %}
                    下一页&gt;
                {% endif %}
            </ul>

            <hr/>
        </form>
    </div>
    <script type="text/javascript">

        {#function run_test_batch(type) {#}
        {#    let csrf = '{{ csrf_token() }}';#}
        {#    run_test_batch_common(type, csrf);#}
        {#}#}
        // 删除项目的 modal
        function invalid(id) {
            {#var csrf_token = "{{ csrf_token() }}";#}
            {#$.ajaxSetup({#}
            {#    beforeSend: function(xhr, settings) {#}
            {#        if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {#}
            {#            xhr.setRequestHeader("X-CSRFToken", csrf_token);#}
            {#        }#}
            {#    }#}
            {#});#}

            $('#my-invalid').modal({
                relatedTarget: this,
                onConfirm : function () {
                    del_data_ajax(id, '{{ url_for("web.delete_project") }}')
                },
                onCancel : function() {

                }

            })
        };


        $('#mode').change(function () {
            if ($('#mode').val() === 'false') {
                $('#report_name').removeAttr("readonly");
            } else {
                $('#report_name').attr('readonly', 'readonly');
            }
        });

        {#function run_test(mode, url, type) {#}
        {#    if (mode === 'batch') {#}
        {#        if ($("input:checked").size() === 0) {#}
        {#            myAlert("请至少选择一个项目运行！");#}
        {#            return;#}
        {#        }#}
        {#        mode = JSON.stringify($("#project_list").serializeJSON());#}
        {#    }#}
        {#    $('#select_env').modal({#}
        {#        relatedTarget: this,#}
        {#        onConfirm: function () {#}
        {#            var data = {#}
                        {#"id": JSON.stringify($("#project_list").serializeJSON()),#}
        {#                "id": mode,#}
        {#                "env_name": $('#env_name').val(),#}
        {#                "type": type,#}
        {#                'report_name': $('#report_name').val(),#}
                        {#'csrf_token': "{{ csrf_token() }}"#}
        {#            };#}
        {#            post('/api/run_test/', data);#}
        {#        },#}
        {#        onCancel: function () {#}
        {#        }#}
        {#    });#}
        {#}#}

        $('#select_all').click(function () {
            var isChecked = $(this).prop("checked");
            $("input[name^='project']").prop("checked", isChecked);
        })
    </script>

{% endblock %}